'''
@project:phpwind
@auther:ZhouQi
@file:request_datas.py
@date:2022/11/22 14:27
@desc:
'''
import pytest
from common.config import config
from common.request_define import RequestsDefine
import requests
import re
import time

# 打开首页
@pytest.fixture()
def open_home():
    home_msg = {
        "home_url": "http://{}/phpwind/".format(config.Host),
        "home_except":"本站新帖"
    }
    return home_msg

#获取token，用于登录
def get_token(init_session_obj):
    res=init_session_obj.create_request(req_type='GET',req_url="http://47.107.178.45/phpwind/")
    csrfToken = 'name="csrf_token" value="(.+?)"/>'  # 定义正则表达式
    csrf_token = init_session_obj.patter_find(csrfToken, res)  #获取token
    return csrf_token

#登录
# 登录参数、断言列表
login_list=[
    {'username':'zhouqi','password':'123456','exce':'成功提示'},     #账号密码正确
    {'username':'zhouqi977','password':'123456','exce':'错误提示'},  #账号错误，密码正确
    {'username':'zhouqi','password':'123456123456','exce':'错误提示'},     #账号正确，密码错误
    {'username':'','password':'123456','exce':'错误提示'},     #账号为空，密码正确
    {'username':'zhouqi','password':'','exce':'错误提示'},     #账号正确，密码为空
    {'username':'','password':'','exce':'错误提示'}      #账号密码为空
]
@pytest.fixture(params=login_list)
def test_login(init_session_obj,request):
    username=request.param.get('username')
    password = request.param.get('password')
    exce = request.param.get('exce')
    login_success = {
            "url": "http://{}/phpwind/index.php?m=u&c=login&a=dologin".format(config.Host),
            "login_data":{"username":username,"password":password,"rememberme":"31536000",
                    "csrf_token":get_token(init_session_obj),"csrf_token":get_token(init_session_obj)},
            "except":exce
        }
    return login_success

#获取登陆状态status
def get_status(init_session_obj):
    login_url = "http://47.107.178.45/phpwind/index.php?m=u&c=login&a=dologin"
    login_data = {"username": "zhouqi", "password": "123456", "rememberme": "31536000",
                  "csrf_token": get_token(init_session_obj),"csrf_token":get_token(init_session_obj)}
    login_res = init_session_obj.create_request(req_type="POST", req_url=login_url, data=login_data)
    status_token = '_statu=(.+?)"'  #登陆状态的正则表达式
    status = init_session_obj.patter_find(status_token, login_res)
    return status
@pytest.fixture()
def test_status(init_session_obj):
    status_msg = {
        "status_url": "http://{}/phpwind/index.php?m=u&c=login".format(config.Host),
        "statu_param" : {"a": "welcome", "_statu": get_status(init_session_obj)},
        "status_except":"zhouqi",
        # "send_url": "http://47.107.178.45/phpwind/index.php?c=post&a=doadd&_json=1&fid=10",
        # "send_data": {"atc_title": "title12345", "atc_content": "content12345", "special": "default",
        #               "reply_notice": "1", "csrf_token": get_token(init_session_obj)},
        # "send_except": "success"
    }
    return status_msg

#发帖
timeString=time.strftime("%y-%m-%d-%H-%M-%S")
titile_String='这是帖子标题'+timeString
content_String='这是帖子内容'+timeString
send_list=[
    {"title1":titile_String,"content1":content_String,"exce1":"success"},  #发帖成功
    {"title1":"。。","content1":content_String,"exce1":"fail"},  #标题为两个特殊符号
    {"title1":"","content1":content_String,"exce1":"fail"},  #标题为空
    {"title1":titile_String,"content1":"..","exce1":"fail"},  #帖子内容为两个特殊符号
    {"title1":titile_String,"content1":"","exce1":"fail"},  #帖子内容为空
    {"title1":"","content1":"","exce1":"fail"}  #标题和帖子内容为空
]
@pytest.fixture(params=send_list)
def test_send(init_session_obj,request):
    title = request.param.get("title1")
    content = request.param.get("content1")
    exce = request.param.get("exce1")
    send_msg={
        "send_url" : "http://47.107.178.45/phpwind/index.php?c=post&a=doadd&_json=1&fid=10",
        "send_data": {"atc_title": title, "atc_content": content, "special": "default",
                     "reply_notice": "1", "csrf_token": get_token(init_session_obj)},
        "except_send": exce
    }
    return send_msg

#回帖
reponse_title = '这是回帖标题' + timeString
reponse_String = '这是回帖内容' + timeString
reply_list=[
    {"title1":reponse_title,"content1":reponse_String,"exce1":"success"},  #回帖成功
    {"title1":".","content1":reponse_String,"exce1":"fail"},  #没有回帖标题
    {"title1":reponse_title,"content1":".","exce1":"fail"}   #没有回帖内容
]
@pytest.fixture(params=reply_list)
def test_reply(init_session_obj,request):
    title = request.param.get("title1")
    content = request.param.get("content1")
    exce = request.param.get("exce1")
    reply_msg={
        "reply_url" : "http://47.107.178.45/phpwind/index.php?c=post&a=doreply&_json=1&fid=10",
        "reply_data" : {"atc_title": title, "atc_content": content,
                        "tid": 31884, "csrf_token": get_token(init_session_obj)},
        "except_reply": exce
    }
    return reply_msg

#退出
@pytest.fixture()
def test_signout():
    out_msg={
        "out_url": "http://47.107.178.45/phpwind/index.php?m=u&c=login&a=logout",
        "exce":"登录"
    }
    return out_msg



